package silo.dungeonbuddy;

import java.awt.Color;
import java.io.BufferedReader;
//import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;

public class RunnableServer implements Runnable
{
    protected Socket clientSocket = null;
    protected String serverText   = null;
    protected String inMessage="";
    protected String outMessage="";
    protected Start game;

    public RunnableServer(Start eGame,Socket clientSocket, String serverText)
    {
        this.clientSocket = clientSocket;
        this.serverText   = serverText;
        game=eGame;
    }// Constructor

    public void run()
    {
        try
        {
            InputStream input  = clientSocket.getInputStream();
            OutputStream output = clientSocket.getOutputStream();
            long time = System.currentTimeMillis();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(input));
            inMessage = inFromClient.readLine();
            inMessage = game.decompress(inMessage);
            outMessage = game.compress(determineOutputMessage());
            output.write(outMessage.getBytes());
            output.close();
            input.close();
            System.out.println("Request processed:  " + "TIME : " + time + "  IN: " + inMessage + "  OUT: " + outMessage);
            try
            {
            	clientSocket.close();
            }catch(Exception e)
            {
            	
            }
        } catch (IOException e)
        {
            //report exception somewhere.
            e.printStackTrace();
        }
    }// Run
    
    public String determineOutputMessage()
	{
    	String mess = inMessage;
		if(mess.contains("!MAP"))
		{
			return game.getBoard().toString();
		}else if(mess.contains("!UPDATE"))
		{
			try
			{
				String userName = "";
				if(!mess.contains("!NO UNIT FROM PLAYER,"))
				{
					int k = 0;
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						userName = userName + mess.charAt(k);
						k = k + 1;
					}//loop
					
					
					//String x="";
					//String y="";
					String name="";
					//String spriteRef="";
					//String sizeX="";
					//String sizeY="";
					//String visible="";
					//String IP;
					//private int squareIndex;
					String row="";
					String column="";
					//String speed="";
					//String movement="";
					//String stealth="";
					//String perception="";
					//String initiative="";
					//String dex="";
					//String maxHp="";
					//String hp="";
					//String turn="";
					
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//x=x+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//y=y+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						name=name+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//spriteRef=spriteRef+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//sizeX=sizeX+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//sizeY=sizeY+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//visible=visible+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					//IP
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						row=row+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						column=column+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					/*while(mess.charAt(k)!=',')
					{
						speed=speed+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						movement=movement+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						stealth=stealth+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						perception=perception+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						initiative=initiative+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						dex=dex+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						maxHp=maxHp+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						hp=hp+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						turn=turn+mess.charAt(k);
						k = k + 1;
					}//loop*/
					/*Unit u = null;
					try
					{
						u = new Unit(name,game,spriteRef,userName,Integer.parseInt(row),Integer.parseInt(column),Integer.parseInt(sizeX),Integer.parseInt(sizeY),Integer.parseInt(maxHp));
					}catch(Exception e)
					{
						u = new Unit(name,game,spriteRef,userName,1,1,1,1,0);
					}
					try
					{
						u.setStealth(Integer.parseInt(stealth));
						u.setPerception(Integer.parseInt(perception));
						u.setHp(Integer.parseInt(hp));
					}catch(Exception e)
					{
						u.setStealth(10);
						u.setPerception(10);
						u.setHp(10);
					}//catch e*/
					for(int i=0;i<game.getUnitList().size();i++)
					{
						try
						{
							Unit u2 = (Unit)game.getUnitList().get(i);
							if(u2.getOwner().equalsIgnoreCase(userName))
							{
								if(!u2.getMoved())
								{
									Vector v = (Vector)game.getBoard().getSquareList().get(Integer.parseInt(row));
									Square sq = (Square)v.get(Integer.parseInt(column));
									u2.moveTo(sq);
									u2.setMoved(false);
									//game.addUnit(u); -- I'm pretty sure this should NOT be here, as this would mean that the map would now have two copies of the player's unit.
								}
								else
								{
									u2.setMoved(false);
								}
								i=game.getUnitList().size();
								try
								{
									boolean inList=false;
									for(int j=0; j<game.getClientList().size(); j++)
									{
										ClientPlayer c = (ClientPlayer)game.getClientList().get(j);
										if(c.getUserName().equals(userName))
										{
											inList=true;
											j=game.getClientList().size();
										}//if(c.getUserName().equals(userName))
									}//next j
									if(!inList)
									{
										game.getClientList().add(new ClientPlayer(userName,u2));
									}//if(!inList)
								}catch(Exception e)
								{
									
								}//catch e
							}//if(u2.getOwner().equalsIgnoreCase(userName))
						}catch(Exception e)
						{
							
						}//catch
					}//next i
				}//if(!mess.contains("!NO UNIT FROM PLAYER,"))
				ClientPlayer cp = null;
				if(game.getClientList().size()>0)
				{
					for(int i=0; i<game.getClientList().size(); i++)
					{
						ClientPlayer cp1 = (ClientPlayer)game.getClientList().get(i);
						if(cp1.getUserName().equals(userName))
						{
							cp=cp1;
						}//if(cp1.getUserName().equals(userName))
					}//next i
				}//if(cp != null)
				String s = "";
				s = game.getUnitList().size() + ",";
				for(int i=0; i<game.getUnitList().size();i++)
				{
					if(cp != null)
					{
						Unit u = (Unit)game.getUnitList().get(i);
						if(u.getForceHideList().size()>0)
						{
							if(u.getForceHideList().contains(cp))
							{
								s = s + u.toStringInvisible();
							}else//if(u.getForceHideList().contains(cp))
							{
								s = s + game.getUnitList().get(i).toString();
							}//else -- if(u.getForceHideList().contains(cp))
						}else//if(u.getForceHideList().size()>0)
						{
							s = s + game.getUnitList().get(i).toString();
						}//else -- if(u.getForceHideList().size()>0)
					}else//if(cp != null)
					{
						s = s + game.getUnitList().get(i).toString();
					}//else -- if(cp != null)
				}//next i
				s = s + game.getTerrainList().size() + ",";
				for(int i=0; i<game.getTerrainList().size();i++)
				{
					s = s + game.getTerrainList().get(i).toString();
				}//next i
				s = s + game.getMarkerList().size() + ",";
				for(int i=0; i<game.getMarkerList().size();i++)
				{
					s = s + game.getMarkerList().get(i).toString();
				}//next i
				s = s + game.getObjectList().size() + ",";
				for(int i=0; i<game.getObjectList().size();i++)
				{
					s = s + game.getObjectList().get(i).toString();
				}//next i
				s = s + game.getEffectList().size() + ",";
				for(int i=0; i<game.getEffectList().size();i++)
				{
					s = s + game.getEffectList().get(i).toString();
				}//next i
				return s;
			}catch(Exception e)
			{
				return "ERROR";
			}//catch
		}else if(mess.contains("!ENDTURN"))
		{
			try
			{
				String userName = "";
				if(!mess.contains("!NO UNIT FROM PLAYER,"))
				{
					int k = 0;
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						userName = userName + mess.charAt(k);
						k = k + 1;
					}//loop
					
					
					//String x="";
					//String y="";
					String name="";
					//String spriteRef="";
					//String sizeX="";
					//String sizeY="";
					//String visible="";
					//String IP;
					//private int squareIndex;
					String row="";
					String column="";
					//String speed="";
					//String movement="";
					//String stealth="";
					//String perception="";
					//String initiative="";
					//String dex="";
					//String maxHp="";
					//String hp="";
					//String turn="";
					
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//x=x+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//y=y+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						name=name+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//spriteRef=spriteRef+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//sizeX=sizeX+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//sizeY=sizeY+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//visible=visible+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					//IP
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						row=row+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						column=column+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					/*while(mess.charAt(k)!=',')
					{
						speed=speed+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						movement=movement+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						stealth=stealth+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						perception=perception+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						initiative=initiative+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						dex=dex+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						maxHp=maxHp+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						hp=hp+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						turn=turn+mess.charAt(k);
						k = k + 1;
					}//loop*/
					/*Unit u = null;
					try
					{
						u = new Unit(name,game,spriteRef,userName,Integer.parseInt(row),Integer.parseInt(column),Integer.parseInt(sizeX),Integer.parseInt(sizeY),Integer.parseInt(maxHp));
					}catch(Exception e)
					{
						u = new Unit(name,game,spriteRef,userName,1,1,1,1,0);
					}
					try
					{
						u.setStealth(Integer.parseInt(stealth));
						u.setPerception(Integer.parseInt(perception));
						u.setHp(Integer.parseInt(hp));
					}catch(Exception e)
					{
						u.setStealth(10);
						u.setPerception(10);
						u.setHp(10);
					}//catch e*/
					for(int i=0;i<game.getUnitList().size();i++)
					{
						try
						{
							Unit u2 = (Unit)game.getUnitList().get(i);
							if(u2.getTurn()==1)
							{
								game.endTurn();
							}//if(u2.getTurn()==1)
							if(u2.getOwner().equalsIgnoreCase(userName))
							{
								if(!u2.getMoved())
								{
									Vector v = (Vector)game.getBoard().getSquareList().get(Integer.parseInt(row));
									Square sq = (Square)v.get(Integer.parseInt(column));
									u2.moveTo(sq);
									u2.setMoved(false);
									//game.addUnit(u); -- I'm pretty sure this should NOT be here, as this would mean that the map would now have two copies of the player's unit.
								}
								else
								{
									u2.setMoved(false);
								}
								i=game.getUnitList().size();
								try
								{
									boolean inList=false;
									for(int j=0; j<game.getClientList().size(); j++)
									{
										ClientPlayer c = (ClientPlayer)game.getClientList().get(j);
										if(c.getUserName().equals(userName))
										{
											inList=true;
											j=game.getClientList().size();
										}//if(c.getUserName().equals(userName))
									}//next j
									if(!inList)
									{
										game.getClientList().add(new ClientPlayer(userName,u2));
									}//if(!inList)
								}catch(Exception e)
								{
									
								}//catch e
							}//if(u2.getOwner().equalsIgnoreCase(userName))
						}catch(Exception e)
						{
							
						}//catch
					}//next i
				}//if(!mess.contains("!NO UNIT FROM PLAYER,"))
				ClientPlayer cp = null;
				if(game.getClientList().size()>0)
				{
					for(int i=0; i<game.getClientList().size(); i++)
					{
						ClientPlayer cp1 = (ClientPlayer)game.getClientList().get(i);
						if(cp1.getUserName().equals(userName))
						{
							cp=cp1;
						}//if(cp1.getUserName().equals(userName))
					}//next i
				}//if(cp != null)
				String s = "";
				s = game.getUnitList().size() + ",";
				for(int i=0; i<game.getUnitList().size();i++)
				{
					if(cp != null)
					{
						Unit u = (Unit)game.getUnitList().get(i);
						if(u.getForceHideList().size()>0)
						{
							if(u.getForceHideList().contains(cp))
							{
								s = s + u.toStringInvisible();
							}else//if(u.getForceHideList().contains(cp))
							{
								s = s + game.getUnitList().get(i).toString();
							}//else -- if(u.getForceHideList().contains(cp))
						}else//if(u.getForceHideList().size()>0)
						{
							s = s + game.getUnitList().get(i).toString();
						}//else -- if(u.getForceHideList().size()>0)
					}else//if(cp != null)
					{
						s = s + game.getUnitList().get(i).toString();
					}//else -- if(cp != null)
				}//next i
				s = s + game.getTerrainList().size() + ",";
				for(int i=0; i<game.getTerrainList().size();i++)
				{
					s = s + game.getTerrainList().get(i).toString();
				}//next i
				s = s + game.getMarkerList().size() + ",";
				for(int i=0; i<game.getMarkerList().size();i++)
				{
					s = s + game.getMarkerList().get(i).toString();
				}//next i
				s = s + game.getObjectList().size() + ",";
				for(int i=0; i<game.getObjectList().size();i++)
				{
					s = s + game.getObjectList().get(i).toString();
				}//next i
				s = s + game.getEffectList().size() + ",";
				for(int i=0; i<game.getEffectList().size();i++)
				{
					s = s + game.getEffectList().get(i).toString();
				}//next i
				return s;
			}catch(Exception e)
			{
				return "ERROR";
			}//catch
		}else if(mess.contains("!MISS"))
		{
			try
			{
				String userName = "";
				String attackeeName="";
				String roll = "";
				String bonus = "";
				int k = 0;
				while(mess.charAt(k)!=',')
				{
					k = k + 1;
				}//loop
				k = k + 1;
				while(mess.charAt(k)!=',')
				{
					attackeeName = attackeeName + mess.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(mess.charAt(k)!=',')
				{
					roll = roll + mess.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(mess.charAt(k)!=',')
				{
					bonus = bonus + mess.charAt(k);
					k = k + 1;
				}//loop
				if(!mess.contains("!NO UNIT FROM PLAYER,"))
				{
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						userName = userName + mess.charAt(k);
						k = k + 1;
					}//loop
					
					
					//String x="";
					//String y="";
					String name="";
					//String spriteRef="";
					//String sizeX="";
					//String sizeY="";
					//String visible="";
					//String IP;
					//private int squareIndex;
					String row="";
					String column="";
					//String speed="";
					//String movement="";
					//String stealth="";
					//String perception="";
					//String initiative="";
					//String dex="";
					//String maxHp="";
					//String hp="";
					//String turn="";
					
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//x=x+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//y=y+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						name=name+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//spriteRef=spriteRef+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//sizeX=sizeX+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//sizeY=sizeY+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//visible=visible+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					//IP
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						row=row+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						column=column+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					/*while(mess.charAt(k)!=',')
					{
						speed=speed+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						movement=movement+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						stealth=stealth+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						perception=perception+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						initiative=initiative+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						dex=dex+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						maxHp=maxHp+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						hp=hp+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						turn=turn+mess.charAt(k);
						k = k + 1;
					}//loop*/
					/*Unit u = null;
					try
					{
						u = new Unit(name,game,spriteRef,userName,Integer.parseInt(row),Integer.parseInt(column),Integer.parseInt(sizeX),Integer.parseInt(sizeY),Integer.parseInt(maxHp));
					}catch(Exception e)
					{
						u = new Unit(name,game,spriteRef,userName,1,1,1,1,0);
					}
					try
					{
						u.setStealth(Integer.parseInt(stealth));
						u.setPerception(Integer.parseInt(perception));
						u.setHp(Integer.parseInt(hp));
					}catch(Exception e)
					{
						u.setStealth(10);
						u.setPerception(10);
						u.setHp(10);
					}//catch e*/
					for(int i=0;i<game.getUnitList().size();i++)
					{
						try
						{
							Unit u2 = (Unit)game.getUnitList().get(i);
							if(u2.getOwner().equalsIgnoreCase(userName))
							{
								if(!u2.getMoved())
								{
									Vector v = (Vector)game.getBoard().getSquareList().get(Integer.parseInt(row));
									Square sq = (Square)v.get(Integer.parseInt(column));
									u2.moveTo(sq);
									u2.setMoved(false);
									//game.addUnit(u); -- I'm pretty sure this should NOT be here, as this would mean that the map would now have two copies of the player's unit.
								}
								else
								{
									u2.setMoved(false);
								}
								i=game.getUnitList().size();
								try
								{
									boolean inList=false;
									for(int j=0; j<game.getClientList().size(); j++)
									{
										ClientPlayer c = (ClientPlayer)game.getClientList().get(j);
										if(c.getUserName().equals(userName))
										{
											inList=true;
											j=game.getClientList().size();
										}//if(c.getUserName().equals(userName))
									}//next j
									if(!inList)
									{
										game.getClientList().add(new ClientPlayer(userName,u2));
									}//if(!inList)
								}catch(Exception e)
								{
									
								}//catch e
							}//if(u2.getOwner().equalsIgnoreCase(userName))
						}catch(Exception e)
						{
							
						}//catch
					}//next i
				}//if(!mess.contains("!NO UNIT FROM PLAYER,"))
				ClientPlayer cp = null;
				if(game.getClientList().size()>0)
				{
					for(int i=0; i<game.getClientList().size(); i++)
					{
						ClientPlayer cp1 = (ClientPlayer)game.getClientList().get(i);
						if(cp1.getUserName().equals(userName))
						{
							cp=cp1;
						}//if(cp1.getUserName().equals(userName))
					}//next i
				}//if(cp != null)
				String s = "";
				s = game.getUnitList().size() + ",";
				for(int i=0; i<game.getUnitList().size();i++)
				{
					if(cp != null)
					{
						Unit u = (Unit)game.getUnitList().get(i);
						if(u.getForceHideList().size()>0)
						{
							if(u.getForceHideList().contains(cp))
							{
								s = s + u.toStringInvisible();
							}else//if(u.getForceHideList().contains(cp))
							{
								s = s + game.getUnitList().get(i).toString();
							}//else -- if(u.getForceHideList().contains(cp))
						}else//if(u.getForceHideList().size()>0)
						{
							s = s + game.getUnitList().get(i).toString();
						}//else -- if(u.getForceHideList().size()>0)
					}else//if(cp != null)
					{
						s = s + game.getUnitList().get(i).toString();
					}//else -- if(cp != null)
				}//next i
				s = s + game.getTerrainList().size() + ",";
				for(int i=0; i<game.getTerrainList().size();i++)
				{
					s = s + game.getTerrainList().get(i).toString();
				}//next i
				s = s + game.getMarkerList().size() + ",";
				for(int i=0; i<game.getMarkerList().size();i++)
				{
					s = s + game.getMarkerList().get(i).toString();
				}//next i
				s = s + game.getObjectList().size() + ",";
				for(int i=0; i<game.getObjectList().size();i++)
				{
					s = s + game.getObjectList().get(i).toString();
				}//next i
				s = s + game.getEffectList().size() + ",";
				for(int i=0; i<game.getEffectList().size();i++)
				{
					s = s + game.getEffectList().get(i).toString();
				}//next i
				Unit un=null;
				for(int i=0;i<game.getUnitList().size();i++)
				{
					Unit u = (Unit)game.getUnitList().get(i);
					if(u.getName().equals(attackeeName))
					{
						un=u;
						i=game.getUnitList().size();
					}//if(u.getName().equals(attackeeName))
				}//next i
				if(un!=null)
				{
					game.getFormList().add(new AttackForm("frmAttack",game,320,240,400,300,"Attack from " + userName,un));
					AttackForm a = (AttackForm)game.getFormList().get(game.getFormList().size()-1);
					a.getLabels().add(new Label("lblAttackMissed",userName + " has missed an attack on " + attackeeName + ".",10,30,480,20,true));
					a.getLabels().add(new Label("lblAttackMissedStats",userName + " rolled a " + roll + " with a bonus of " + bonus + ".",10,60,480,20,true));
					a.getButtons().add(new Button(game,270,320,true,50,"Okay",Color.getHSBColor(57, 52, 84),true));
				}//if(un!=null)
				return s;
			}catch(Exception e)
			{
				return "ERROR";
			}//catch
		}else if(mess.contains("!HIT"))
		{
			try
			{
				String userName = "";
				String attackeeName="";
				String roll = "";
				String bonus = "";
				String damage = "";
				int k = 0;
				while(mess.charAt(k)!=',')
				{
					k = k + 1;
				}//loop
				k = k + 1;
				while(mess.charAt(k)!=',')
				{
					attackeeName = attackeeName + mess.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(mess.charAt(k)!=',')
				{
					roll = roll + mess.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(mess.charAt(k)!=',')
				{
					bonus = bonus + mess.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(mess.charAt(k)!=',')
				{
					damage = damage + mess.charAt(k);
					k = k + 1;
				}//loop
				if(!mess.contains("!NO UNIT FROM PLAYER,"))
				{
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						userName = userName + mess.charAt(k);
						k = k + 1;
					}//loop
					
					
					//String x="";
					//String y="";
					String name="";
					//String spriteRef="";
					//String sizeX="";
					//String sizeY="";
					//String visible="";
					//String IP;
					//private int squareIndex;
					String row="";
					String column="";
					//String speed="";
					//String movement="";
					//String stealth="";
					//String perception="";
					//String initiative="";
					//String dex="";
					//String maxHp="";
					//String hp="";
					//String turn="";
					
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//x=x+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//y=y+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						name=name+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//spriteRef=spriteRef+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//sizeX=sizeX+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//sizeY=sizeY+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						//visible=visible+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					//IP
					while(mess.charAt(k)!=',')
					{
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						row=row+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						column=column+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					/*while(mess.charAt(k)!=',')
					{
						speed=speed+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						movement=movement+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						stealth=stealth+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						perception=perception+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						initiative=initiative+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						dex=dex+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						maxHp=maxHp+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						hp=hp+mess.charAt(k);
						k = k + 1;
					}//loop
					k = k + 1;
					while(mess.charAt(k)!=',')
					{
						turn=turn+mess.charAt(k);
						k = k + 1;
					}//loop*/
					/*Unit u = null;
					try
					{
						u = new Unit(name,game,spriteRef,userName,Integer.parseInt(row),Integer.parseInt(column),Integer.parseInt(sizeX),Integer.parseInt(sizeY),Integer.parseInt(maxHp));
					}catch(Exception e)
					{
						u = new Unit(name,game,spriteRef,userName,1,1,1,1,0);
					}
					try
					{
						u.setStealth(Integer.parseInt(stealth));
						u.setPerception(Integer.parseInt(perception));
						u.setHp(Integer.parseInt(hp));
					}catch(Exception e)
					{
						u.setStealth(10);
						u.setPerception(10);
						u.setHp(10);
					}//catch e*/
					for(int i=0;i<game.getUnitList().size();i++)
					{
						try
						{
							Unit u2 = (Unit)game.getUnitList().get(i);
							if(u2.getOwner().equalsIgnoreCase(userName))
							{
								if(!u2.getMoved())
								{
									Vector v = (Vector)game.getBoard().getSquareList().get(Integer.parseInt(row));
									Square sq = (Square)v.get(Integer.parseInt(column));
									u2.moveTo(sq);
									u2.setMoved(false);
									//game.addUnit(u); -- I'm pretty sure this should NOT be here, as this would mean that the map would now have two copies of the player's unit.
								}
								else
								{
									u2.setMoved(false);
								}
								i=game.getUnitList().size();
								try
								{
									boolean inList=false;
									for(int j=0; j<game.getClientList().size(); j++)
									{
										ClientPlayer c = (ClientPlayer)game.getClientList().get(j);
										if(c.getUserName().equals(userName))
										{
											inList=true;
											j=game.getClientList().size();
										}//if(c.getUserName().equals(userName))
									}//next j
									if(!inList)
									{
										game.getClientList().add(new ClientPlayer(userName,u2));
									}//if(!inList)
								}catch(Exception e)
								{
									
								}//catch e
							}//if(u2.getOwner().equalsIgnoreCase(userName))
						}catch(Exception e)
						{
							
						}//catch
					}//next i
				}//if(!mess.contains("!NO UNIT FROM PLAYER,"))
				ClientPlayer cp = null;
				if(game.getClientList().size()>0)
				{
					for(int i=0; i<game.getClientList().size(); i++)
					{
						ClientPlayer cp1 = (ClientPlayer)game.getClientList().get(i);
						if(cp1.getUserName().equals(userName))
						{
							cp=cp1;
						}//if(cp1.getUserName().equals(userName))
					}//next i
				}//if(cp != null)
				String s = "";
				s = game.getUnitList().size() + ",";
				for(int i=0; i<game.getUnitList().size();i++)
				{
					if(cp != null)
					{
						Unit u = (Unit)game.getUnitList().get(i);
						if(u.getForceHideList().size()>0)
						{
							if(u.getForceHideList().contains(cp))
							{
								s = s + u.toStringInvisible();
							}else//if(u.getForceHideList().contains(cp))
							{
								s = s + game.getUnitList().get(i).toString();
							}//else -- if(u.getForceHideList().contains(cp))
						}else//if(u.getForceHideList().size()>0)
						{
							s = s + game.getUnitList().get(i).toString();
						}//else -- if(u.getForceHideList().size()>0)
					}else//if(cp != null)
					{
						s = s + game.getUnitList().get(i).toString();
					}//else -- if(cp != null)
				}//next i
				s = s + game.getTerrainList().size() + ",";
				for(int i=0; i<game.getTerrainList().size();i++)
				{
					s = s + game.getTerrainList().get(i).toString();
				}//next i
				s = s + game.getMarkerList().size() + ",";
				for(int i=0; i<game.getMarkerList().size();i++)
				{
					s = s + game.getMarkerList().get(i).toString();
				}//next i
				s = s + game.getObjectList().size() + ",";
				for(int i=0; i<game.getObjectList().size();i++)
				{
					s = s + game.getObjectList().get(i).toString();
				}//next i
				s = s + game.getEffectList().size() + ",";
				for(int i=0; i<game.getEffectList().size();i++)
				{
					s = s + game.getEffectList().get(i).toString();
				}//next i
				Unit un=null;
				for(int i=0;i<game.getUnitList().size();i++)
				{
					Unit u = (Unit)game.getUnitList().get(i);
					if(u.getName().equals(attackeeName))
					{
						un=u;
						i=game.getUnitList().size();
					}//if(u.getName().equals(attackeeName))
				}//next i
				if(un!=null)
				{
					game.getFormList().add(new AttackForm("frmAttack",game,320,240,400,300,"Attack from " + userName,un));
					AttackForm a = (AttackForm)game.getFormList().get(game.getFormList().size()-1);
					a.getLabels().add(new Label("lblAttackHit",userName + " has hit with an attack on " + attackeeName + ".",10,30,480,20,true));
					a.getLabels().add(new Label("lblAttackHitStats",userName + " rolled a " + roll + " with a bonus of " + bonus + ".",10,60,480,20,true));
					a.getLabels().add(new Label("lblAttackHitDamage","This attack will do " + damage + " damage to " + attackeeName + ".",10,90,480,20,true));
					a.getButtons().add(new Button(game,320,270,true,50,"Accept",Color.getHSBColor(57, 52, 84),true));
					a.getButtons().add(new Button(game,240,270,true,50,"Reject",Color.getHSBColor(57, 52, 84),true));
					try
					{
						a.setDamage(Integer.parseInt(damage));
					}catch(Exception e2)
					{
						a.setDamage(0);
					}//catch e2
				}//if(un!=null)
				return s;
			}catch(Exception e)
			{
				return "ERROR";
			}//catch
		}else
		{
			return "DEFAULT";
		}
	}//determineOutputMessage()
}//class RunnableServer